package site.addzero.repl

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import org.koin.core.annotation.Single
import site.addzero.app.AdvancedRepl
import site.addzero.app.ParamDef
import site.addzero.cli.dotfiles.dotfilesService
import site.addzero.cli.platform.PlatformTools

/**
 * SyncCommand的REPL包装类，实现AdvancedRepl接口
 */
@Single

class SynCRepl : AdvancedRepl<Unit, Unit> {
    override val command: String = "sync"
    override val description: String =
        """同步""".trimMargin()
    override val paramDefs: List<ParamDef> = listOf()

    override fun eval(params: Unit) = runBlocking {
        val readBoolWithFlase = PlatformTools.readBoolWithFlase(
            "(注意此操作是在一台崭新的机器上运行的操作!!!,且需要用户已经配置过远程仓库,会拉取最新配置,然后创建符号链接指向家目录,\n" +
                    "            |符号链接会以仓库为准覆盖已有文件\n" +
                    "            |谨慎操作!!!,是否同步 ?"
        )
        if (readBoolWithFlase == false) {
            println("取消同步")
        }

        withContext(Dispatchers.Default) {
            val success = dotfilesService.sync()
            if (success) {
                println("同步成功")
            } else {
                println("同步失败")
            }
        }
    }

//    override fun createParams(values: List<Any?>) {
//        return
//    }
}
